package cz.mendelu.swi2.skladDilu.controllers;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;


import cz.mendelu.swi2.skladDilu.Inventory;
import cz.mendelu.swi2.skladDilu.Warehouse;

@Controller
@RequestMapping("/skladdilu/inventory")
public class CInventorySkladDiluController {
	@Autowired   
	public Warehouse warehouse;

	@RequestMapping(method = RequestMethod.GET)
	public ModelAndView sklad() {
        ModelAndView mview = new ModelAndView("inventory") ;
        mview.addObject("listDilu", warehouse.getListOfParts()) ;
        return mview ;
	}


	@RequestMapping(method = RequestMethod.POST)
	public ModelAndView processForm(HttpServletRequest request, HttpServletResponse response) {
	      Inventory inventura = new Inventory(warehouse);
	      @SuppressWarnings("unchecked")
		  Map<String, String[]> parameters = request.getParameterMap();
		  ModelAndView mview = null;
	     
	    
	      if(!inventura.processInventory(parameters))
	      {
	    	  mview=new ModelAndView("inventory") ;
	    	  mview.addObject("message","Chyba vstupnicha dat pro inventuru");
				mview.addObject("error",true);
				 mview.addObject("listDilu", warehouse.getListOfParts()) ;
	      
	      }
	      else
	      {
	    	  new ModelAndView("skladDilu") ;
	    	  mview.addObject("inventura",true);
	    	  mview.addObject("listDilu", warehouse.getListOfParts()) ;
	      }
	      return mview ;
	      
	}

  }
